//
//  NSStringVerify.m
//  RegisterModule
//
//  Created by 吴月 on 14-12-8.
//  Copyright (c) 2014年 chenTengFei. All rights reserved.
//

#import "NSString+Verify.h"

@implementation NSString (Verify)

/*邮箱验证 MODIFIED BY HELENSONG*/
+ (BOOL)tf_isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/*手机号码验证 MODIFIED BY HELENSONG*/
+ (BOOL)tf_isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,170-9
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,147
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|47|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|7[09]|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        if([regextestcm evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Mobile");
        } else if([regextestct evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Telecom");
        } else if ([regextestcu evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Unicom");
        } else {
            NSLog(@"Unknow");
        }
        
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)tf_isValidateShenFenZheng:(NSString *)shenFenZheng
{
    NSString *emailRegex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:shenFenZheng];
    
}

+ (BOOL)tf_isValidateBankNum:(NSString *)bankNum
{
    //^[u4e00-u9fa5],{0,}$
    NSString *emailRegex = @"^(\\d{16}|\\d{19})$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:bankNum];
    
}

+ (BOOL)tf_isValidateChineseFirst:(NSString *)Chinese
{
    //是否以中文开头(unicode中文编码范围是0x4e00~0x9fa5)
    int utfCode = 0;
    void *buffer = &utfCode;
    NSRange range = NSMakeRange(0, 1);
    
    BOOL b = [Chinese getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
    if (b && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))
        return YES;
    else
        return NO;
}

+ (BOOL)tf_isSimpleMobileNumber:(NSString *)mobileNum {
    if (mobileNum.length == 11) {
        return YES;
    } else {
        return NO;
    }
}
/**
 *  校验密码的格式是否符合要求
 * ^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$ -- 意思是必须包含数字和字母－长度要求6到20位
 *  
 * ^[A-Za-z0-9]{6,20}$  意思是包含数字或字母 －－长度要求6到20位
 *
 *  @param password 密码
 *
 *  @return YES 符合格式 NO不符合
 */
+ (BOOL)judgePasswordFormat:(NSString *)password
{
    NSString *regex = @"^[A-Za-z0-9]{6,20}$";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([predicate evaluateWithObject:password] == YES) {
        //implement

        return YES;
    }else{
        return NO;
    }
}
/**
 *  校验 －－密码 意思是包含数字和字母（同时存在）
 *
 *  @param password
 *
 *  @return
 */
+ (BOOL)judgePasswordLetterAndNumber:(NSString *)password
{
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([predicate evaluateWithObject:password] == YES) {
        //implement
        
        return YES;
    }else{
        return NO;
    }}
/**
 *  校验 －－密码 意思是包含数字或字母
 *
 *  @param password
 *
 *  @return
 */
+ (BOOL)judgePasswordLetterOrNumber:(NSString *)password
{
    NSString *regex = @"^[A-Za-z0-9]{6,20}$";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([predicate evaluateWithObject:password] == YES) {
        //implement
        
        return YES;
    }else{
        return NO;
    }
}
//校验文件是否可以打开
+ (BOOL)judgeCanOpenFileStr:(NSString *)fileStr
{
    NSString *lowerString = [fileStr lowercaseString];
    //图片
    if ([lowerString hasSuffix:@"jpeg"]||[lowerString hasSuffix:@"png"]||[lowerString hasSuffix:@"jpg"]) {
        return YES;
    }else if ([lowerString hasSuffix:@"pdf"]){
        //pdf文档
        return YES;
    }else if ([lowerString hasSuffix:@"pptx"]){
        //ppt文档
        return YES;
    }else if ([lowerString hasSuffix:@"xlsx"]||[lowerString hasSuffix:@"xls"]){
        //excle文档
        return YES;
    }else if ([lowerString hasSuffix:@"doc"]||[lowerString hasSuffix:@"docx"]){
        //doc文档
        return YES;
    }else if ([lowerString hasSuffix:@"txt"]){
        //text文档
        return YES;
    }
    return NO;
}
//校验是否全是空格
+ (BOOL)IsEmptySpace:(NSString *)string
{
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimedString = [string stringByTrimmingCharactersInSet:set];
    if (trimedString.length != 0  ) {
        return YES;
    }
    return NO;
}

@end

